package org.xiaojinlong.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Created by jinlong on 2015/2/13.
 */
public class UDPEchoServer {
    static final int ECHOMAX = 255;

    public static void main(String[] args) throws IOException {
        int serverPort = 5555;
        DatagramSocket socket = new DatagramSocket(serverPort);
        DatagramPacket packet = new DatagramPacket(new byte[ECHOMAX], ECHOMAX);

        while (true) {
            socket.receive(packet);
            System.out.println("Handling client at " + packet.getAddress().getHostAddress()
                                        + " on port " + packet.getPort());
            socket.send(packet);
            packet.setLength(ECHOMAX); // 重新设置长度以避免缓冲区缩小

        }

    }
}
